<?php
/*
---------------------------------------------------------
														+
名称:易通支付通道										+
														+
控制器名称:YiTongController								+
														+
日期:2017-10-11											+
														+
作者:小翁												+
														+
---------------------------------------------------------
*/
namespace Pay\Controller;

use Common\Common\Api\Order;
use Common\Common\Api\Ordermember;
use Common\Common\Api\Redis\Channel;
use Common\Common\Api\Redis\Product;

class Huoniao2Controller extends PayController{
    protected $config;
    protected $key;
    protected $url;
    protected $class;
    public function __construct(){
        parent::__construct();
        $classes = explode("\\",__CLASS__);
        $class = end($classes);
        $this->class = substr($class, 0, stripos($class, "Controller"));
    }

    public function pay($array){
        $this->config = Channel::getInstance()->getById($array['channel']);
        if(!$this->config){
            $this->showmessage('通道错误!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $product = Product::getInstance()->getById(I("request.pay_bankcode",0,"intval"));
        if(!$product){
            $this->showmessage('未找到支付产品!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $parameter = array(
            'code' => $this->config['code'], // 通道名称
            'title' => $this->config['title'],
            'exchange' => 1, // 金额比例
            'gateway' => $this->config['gateway'],
            'orderid' => createOrderId(),
            'out_trade_id' => I("request.pay_orderid"),
            'body' => 'pay',
            'channel'=>$array
        );

        $return = $this->orderadd( $parameter );

        $ismobile = isMobile();

        $data = [];
        $data['merchantCode'] = $this->config['mch_id'];
        $data['amount'] = $return['amount'];
        $data['orderNumber'] = $return['orderid'];
        switch($this->config['paytype']){
            case 1:
                $data['payCode'] = 'D1_WX_SCAN';
                break;
            default:
                $this->showmessage('不支持此产品!',['code'=>C("THIRDCODES.".$this->class)]);
        }
        $data['submitTime'] = date("YmdHis",time());
        $data['commodityName'] = 'zr';
        $data['submitIp'] = getIP();
        $data['syncRedirectUrl'] = $return['callbackurl'];
        $data['asyncNotifyUrl'] = $return['notifyurl'];
        $data['remark'] = 'zr';

        $data['sign'] = $this->getSign($data);

        $res = curl($this->config['gateway'], httpstr($data));
        $res = json_decode($res, true);


        //echo '<pre/>'; print_r($data);print_r($res); exit;

        $code = $res['content'];

        if(!$code){
            if($res['message']){
                $this->showmessage($res['message'],['code'=>C("THIRDCODES.".$this->class)]);
            }
            $this->showmessage('获取支付链接失败!',['code'=>C("THIRDCODES.".$this->class)]);
        }


        $paytype = transArr( C("PAYTYPES"), "id");
        $this->assign("wap_url", $code);
        $this->assign("imgurl", C("website.DOMAIN")."/Pay_Qrcode_getCode?type=2&data=" . base64_encode($code));
        $this->assign("title", $paytype[$this->config['paytype']]);
        $this->assign("return", $return);


        switch($this->config['paytype']){
            //微信
            case 1:
                header("Location:{$code}");
                break;
            case 2:
                header("Location:{$code}");
                break;
            case 3:
            case 4:
            case 17:
            case 18:
                header("Location:{$code}");
                break;
            default:
                $this->display("WeiXin/Pay");
        }
    }

    private function getSign($data){
        ksort($data);

        $str = "";
        foreach($data as $k=>$v){
            if($k === 'sign' || $v === '' || $v === null){
                continue;
            }

            $str .= "{$k}={$v}&";
        }

        $str = substr($str,0,-1) . $this->config['signkey'];

        return strtoupper(md5($str));
    }

    //通知
    public function notifyurl(){
		//$this->debug_callback();
		
        $p = I("post.");

        /*$p = array (
            'merchantCode' => '00B19040618020902500',
            'amount' => '200.00',
            'trxNo' => '00719040620063339700',
            'orderNumber' => '2019040620063334025',
            'submitTime' => '20190406200634',
            'payTime' => '20190406200753',
            'sign' => '803cad48569d072a7a915fbe302d74d2',
            'remark' => 'zr',
            'payCode' => 'D0_WX_SCAN',
            'payStatus' => 'orderPaid',
            'commodityName' => 'zr',
        );*/

        //获取订单信息
        $orderid = $p['orderNumber'];
        $find = $this->checkOrderNotify($orderid);

        if(!$find){
            echo 'order_not_found'; exit;
        }
        //保存回调数据
        M("Order")->where(array("pay_orderid"=>$p['busi_order_no']))->save(array(

            "pay_orderid_rd"=>htmlspecialchars($p['trxNo'])));

        //获取渠道信息
        $this->config = M("Channel")->where(array('code'=>$this->class))->find();

        $sign = strtoupper($p['sign']);
        $sign2 = $this->getSign($p);

        if($sign !== $sign2){
            exit("sign_wrong");
        }
        if($p['payStatus'] !== 'orderPaid'){
            exit("pay_fail");
        }

        $this->async($find['pay_orderid'],'success');
    }

    public function callBackUrl(){

        echo '支付成功'; exit;
    }
}